package com.xzw.cameratest.activty;


import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Process;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;

import com.android.business.common.PushMessageCode;
import com.xzw.cameratest.R;
import com.xzw.cameratest.base.BaseActivity;
import com.xzw.cameratest.fragment.OrganizationTreeFragment;
import com.xzw.cameratest.push.DSSPush;
import com.xzw.cameratest.push.PushCallbackInterface;
import com.xzw.cameratest.push.ReconnectService;



public class OrganizationTreeActivity extends BaseActivity implements PushCallbackInterface {


    private long firstTime = 0;
    private ReconnectService mReconnectService;
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            ReconnectService.ReconnectBinder mBinder = (ReconnectService.ReconnectBinder) service;
            mReconnectService = mBinder.getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected int initContentView() {
        return R.layout.activity_organization_tree;
    }

    @Override
    protected void initView() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.container,new OrganizationTreeFragment()).commit();
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void doSomething() {
        DSSPush dssPush = new DSSPush();
        dssPush.init(this);
        dssPush.setPushWatcher(this);
        Intent reconnectIntent = new Intent(this, ReconnectService.class);
        bindService(reconnectIntent, serviceConnection, BIND_AUTO_CREATE);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        long secondTime = System.currentTimeMillis();
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (secondTime - firstTime > 2000) {
                firstTime = System.currentTimeMillis();
                showToast(getString(R.string.press_again_exit_app));
            }else {
                System.exit(0);
                Process.killProcess(Process.myPid());
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void callback(Context context, Intent intent, int cmd, String method, String content) {
        if (cmd == PushMessageCode.DPSDK_CMD_CMS_CLOSE.getValue()) {
            mReconnectService.reconnectServe();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(serviceConnection);
    }
}
